<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";
import { getBarData } from "@/api/chart";
import { notify } from "@/utils/message";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "200px",
    },
    height: {
      type: String,
      default: "200px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      getBarData()
        .then((res) => {
          notify(1, "获取柱状图数据");
          this.chart.setOption({
            title: {
              text: "图书价格分布",
              subtext: "范围（排名前十）",
              left: "center",
            },
            tooltip: {
              trigger: "axis",
              axisPointer: {
                type: "cross",
                crossStyle: {
                  color: "#999",
                },
              },
            },
            toolbox: {
              feature: {
                dataView: { show: true, readOnly: false },
                magicType: { show: true, type: ["line", "bar"] },
                restore: { show: true },
                saveAsImage: { show: true },
              },
            },
            legend: {
              top: "10%",
              data: ["价格(柱状图)", "价格(折线图)"],
            },
            xAxis: [
              {
                type: "category",
                data: res.data.data.map((book) => book.name.substring(0, 20)), // TODO
                axisPointer: {
                  type: "shadow",
                },
                axisLabel: {
                  rotate: 25,
                },
              },
            ],

            yAxis: [
              {
                type: "value",
                name: "价格（元）",
                min: 100,
                max: 2500,
                interval: 200,
                axisLabel: {
                  formatter: "{value} ",
                },
              },
              {
                type: "value",
                name: "价格（元）",
                min: 100,
                max: 2500,
                interval: 200,
                axisLabel: {
                  formatter: "{value} ",
                },
              },
            ],
            grid: {
              left: "10%",
              right: "10%",
              bottom: "3%",
              top: "15%",
              containLabel: true,
            },
            series: [
              {
                name: "价格(柱状图)",
                type: "bar",
                tooltip: {
                  valueFormatter: function (value) {
                    return value + " 元";
                  },
                },
                itemStyle: {
                  normal: {
                    //这里是重点
                    color: function (params) {
                      var colorList = [
                        "#c23531",
                        "#2f4554",
                        "#61a0a8",
                        "#d48265",
                        "#91c7ae",
                        "#749f83",
                        "#2f4554",
                        "#ca8622",
                        "#61a0a8",
                        "#c23531",
                        "#2f4554",
                        "#2f4554",
                      ];
                      return colorList[params.dataIndex];
                    },
                  },
                },
                data: res.data.data.map((book) => book.price), // TODO
              },
              {
                name: "价格(折线图)",
                type: "line",
                tooltip: {
                  valueFormatter: function (value) {
                    return value + " 元";
                  },
                },
                data: res.data.data.map((book) => book.price), // TODO
              },
            ],
          });
        })
        .catch((err) => {
          notify(2, "获取柱状图数据", err);
        });
    },
  },
};
</script>
